package gspushmq.v1.consumer.core;

import ggframework.util.GGLogger;
import gspushmq.model.Data;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Description 消息存储队列
 * @Author 成尚佳
 * @Date 2019/1/17 14:59
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public enum MessageQueue {

    messageQueue;

    private BlockingQueue<Data> blockingQueue = new LinkedBlockingQueue<>();

    public void add (Data data){
        if(blockingQueue.size() >= 2000){
            GGLogger.info(MessageQueue.class, "队列：" + this.name() + "    " + blockingQueue.size());
        }
        try {
            //生产程不阻塞
            blockingQueue.add(data);
        }catch (Exception e){
            GGLogger.error(MessageQueue.class,this.name()+"队列溢出--"+e);
        }
    }

    public Data take(){
        try {
            //消费线程可以阻塞
            return blockingQueue.take();
        }catch (InterruptedException e){
            GGLogger.error(MessageQueue.class,this.name()+"队列无消息--"+e);
        }
        return null;
    }
}
